package main

import "fmt"

type Cat struct {
	Name string
	Age  int
}

func main() {
	var intChan chan int
	intChan = make(chan int, 3)
	fmt.Printf("intChan=%v\n", intChan)
	//写入数据
	intChan <- 10
	num := 211
	intChan <- num
	fmt.Printf("channel len=%v cap = %v\n", len(intChan), cap(intChan))
	//读取
	var num2 int = <-intChan
	fmt.Printf("channel len=%v cap = %v\n", len(intChan), cap(intChan))
	fmt.Printf("num2=%d\n", num2)

	allChan := make(chan interface{}, 3)
	allChan <- 10
	allChan <- "tom jack"
	cat := Cat{"小花猫", 4}
	allChan <- cat
	<-allChan
	<-allChan
	newCat := <-allChan
	a := newCat.(Cat)
	fmt.Printf("newCat.Name=%v", a)

	var chan1 chan int
	chan1 = make(chan int, 3)
	chan1 <- 100
	chan1 <- 200
	close(chan1)

	//关闭channel后只读不进

	//channel遍历
	intchan2 := make(chan int, 100)
	for i := 0; i < 100; i++ {
		intchan2 <- i * 2
	}
	//遍历只能用for-range  遍历前应关闭管道
	close(intchan2)
	for v := range intchan2 {
		fmt.Printf("v=%d", v)
	}

}
